{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-12-26T01:39:04.661396Z",
     "start_time": "2024-12-26T01:39:04.647312Z"
    }
   },
   "source": [
    ""
   ],
   "outputs": [],
   "execution_count": 79
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:39:04.802536Z",
     "start_time": "2024-12-26T01:39:04.798351Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "import matplotlib.pyplot as plt"
   ],
   "id": "9c201dd5d1296eae",
   "outputs": [],
   "execution_count": 80
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:39:06.762548Z",
     "start_time": "2024-12-26T01:39:04.820704Z"
    }
   },
   "cell_type": "code",
   "source": [
    "raw_df_15 = pd.read_csv('./data/15_05.csv')\n",
    "raw_df_15 = raw_df_15[raw_df_15['label'] == 0]\n",
    "raw_df_21 = pd.read_csv('./data/21_05.csv')\n",
    "raw_df_21 = raw_df_21[raw_df_21['label'] == 0]\n",
    "\n"
   ],
   "id": "9f32f6ea7026257c",
   "outputs": [],
   "execution_count": 81
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:39:06.778741Z",
     "start_time": "2024-12-26T01:39:06.763571Z"
    }
   },
   "cell_type": "code",
   "source": "drop_columns = ['time', 'group', 'new_group', \"label\"] + ['r_square', 'r_wind_speed_to_power', 'torque', 'cp', 'ct']",
   "id": "a680e05cbce1a759",
   "outputs": [],
   "execution_count": 82
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:39:06.809074Z",
     "start_time": "2024-12-26T01:39:06.779747Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df_15 = raw_df_15.drop(drop_columns, axis=1) \n",
    "df_21 = raw_df_21.drop(drop_columns, axis=1)"
   ],
   "id": "b99e5013a9bbff74",
   "outputs": [],
   "execution_count": 83
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 相关性分析",
   "id": "3a127e085242c69c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:39:06.824218Z",
     "start_time": "2024-12-26T01:39:06.810108Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def correlation_analyse(df):\n",
    "    corr = df.corr()\n",
    "    target_corr = corr['power'].drop('power')\n",
    "    target_corr = target_corr.abs().sort_values(ascending=False)\n",
    "    \n",
    "    filtered_features = target_corr[target_corr > 0.1]\n",
    "    print(filtered_features)\n",
    "    return filtered_features"
   ],
   "id": "ac76e66796ac8365",
   "outputs": [],
   "execution_count": 84
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:39:07.395798Z",
     "start_time": "2024-12-26T01:39:06.825237Z"
    }
   },
   "cell_type": "code",
   "source": [
    "corr_features_15 = correlation_analyse(df_15)\n",
    "print(len(corr_features_15))"
   ],
   "id": "64f38e0835cdde0f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wind_speed             0.943621\n",
      "generator_speed        0.898265\n",
      "pitch_moto_tmp_sd      0.654357\n",
      "pitch_speed_sd         0.539675\n",
      "wind_speed_cube        0.515524\n",
      "pitch3_angle           0.498147\n",
      "pitch1_angle           0.497697\n",
      "pitch_angle_mean       0.497153\n",
      "pitch2_angle           0.495371\n",
      "wind_speed_square      0.415630\n",
      "pitch1_moto_tmp        0.411400\n",
      "pitch_moto_tmp_mean    0.392390\n",
      "pitch3_moto_tmp        0.383987\n",
      "pitch2_moto_tmp        0.380041\n",
      "acc_x                  0.217666\n",
      "tmp_diff               0.196615\n",
      "int_tmp                0.189426\n",
      "pitch_angle_sd         0.182143\n",
      "environment_tmp        0.138091\n",
      "acc_y                  0.133184\n",
      "Name: power, dtype: float64\n",
      "20\n"
     ]
    }
   ],
   "execution_count": 85
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:39:07.674948Z",
     "start_time": "2024-12-26T01:39:07.395798Z"
    }
   },
   "cell_type": "code",
   "source": [
    "corr_features_21 = correlation_analyse(df_21)\n",
    "print(len(corr_features_21))"
   ],
   "id": "ff06a1de5737ee19",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wind_speed                         0.931066\n",
      "generator_speed                    0.848476\n",
      "pitch_moto_tmp_sd                  0.618726\n",
      "wind_speed_square                  0.579720\n",
      "pitch_speed_sd                     0.552614\n",
      "pitch3_moto_tmp                    0.526446\n",
      "pitch_moto_tmp_mean                0.516279\n",
      "pitch2_moto_tmp                    0.514250\n",
      "wind_speed_cube                    0.513495\n",
      "pitch1_moto_tmp                    0.506920\n",
      "pitch3_angle                       0.496455\n",
      "pitch2_angle                       0.495411\n",
      "pitch_angle_mean                   0.494844\n",
      "pitch1_angle                       0.492600\n",
      "r_wind_speed_to_generator_speed    0.312183\n",
      "int_tmp                            0.267836\n",
      "acc_x                              0.267719\n",
      "environment_tmp                    0.225334\n",
      "pitch_angle_sd                     0.201216\n",
      "pitch2_ng5_tmp                     0.148290\n",
      "pitch3_ng5_DC                      0.135109\n",
      "pitch1_ng5_DC                      0.129842\n",
      "Name: power, dtype: float64\n",
      "22\n"
     ]
    }
   ],
   "execution_count": 86
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:39:07.690095Z",
     "start_time": "2024-12-26T01:39:07.675987Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 交集\n",
    "corr_features = set(corr_features_15.index).intersection(set(corr_features_21.index))\n",
    "print(len(corr_features)) \n",
    "print(corr_features)"
   ],
   "id": "a3dbcaffc0ff5af",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n",
      "{'pitch_moto_tmp_mean', 'int_tmp', 'acc_x', 'pitch1_moto_tmp', 'wind_speed_square', 'generator_speed', 'pitch_speed_sd', 'pitch_angle_mean', 'wind_speed_cube', 'pitch2_angle', 'pitch_moto_tmp_sd', 'wind_speed', 'pitch1_angle', 'pitch3_moto_tmp', 'pitch_angle_sd', 'environment_tmp', 'pitch2_moto_tmp', 'pitch3_angle'}\n"
     ]
    }
   ],
   "execution_count": 87
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 单变量特征选择",
   "id": "d67f1380a243d852"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:39:07.705431Z",
     "start_time": "2024-12-26T01:39:07.691129Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.feature_selection import SelectKBest, chi2, mutual_info_regression\n",
    "from sklearn.preprocessing import MinMaxScaler"
   ],
   "id": "199d0fdad60b1a2f",
   "outputs": [],
   "execution_count": 88
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:39:07.721563Z",
     "start_time": "2024-12-26T01:39:07.706454Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def univariate_feature_selection(df):\n",
    "    # 提取特征和标签\n",
    "    X = df.drop('power', axis=1)\n",
    "    y = df['power']\n",
    "    \n",
    "    # 将特征缩放到 [0, 1] 范围\n",
    "    scaler = MinMaxScaler()\n",
    "    X_scaled = scaler.fit_transform(X)\n",
    "    \n",
    "    # 使用互信息进行特征选择\n",
    "    mi_selector = SelectKBest(score_func=mutual_info_regression, k='all')\n",
    "    mi_selector.fit(X, y)\n",
    "    \n",
    "    # 获取所有特征的得分\n",
    "    mi_scores = mi_selector.scores_\n",
    "    \n",
    "    # 设置分数阈值\n",
    "    threshold_mi = 0.4  # 设定的分数阈值\n",
    "    selected_features_mi = X.columns[mi_scores > threshold_mi]\n",
    "    selected_scores_mi = mi_scores[mi_scores > threshold_mi]\n",
    "    \n",
    "    # 创建 DataFrame 并排序\n",
    "    mi_results = pd.DataFrame({'Feature': selected_features_mi, 'Score': selected_scores_mi})\n",
    "    mi_results = mi_results.sort_values(by='Score', ascending=False)\n",
    "    \n",
    "    # 打印特征及其分数\n",
    "    print(\"根据分数阈值选择的特征及其分数（互信息）:\")\n",
    "    print(mi_results)\n",
    "    \n",
    "    return selected_features_mi"
   ],
   "id": "62ee9da39d9f055f",
   "outputs": [],
   "execution_count": 89
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:39:53.870161Z",
     "start_time": "2024-12-26T01:39:07.722599Z"
    }
   },
   "cell_type": "code",
   "source": "mi_features_15 = univariate_feature_selection(df_15)",
   "id": "cbdb99bf86e89606",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "根据分数阈值选择的特征及其分数（互信息）:\n",
      "                            Feature     Score\n",
      "1                   generator_speed  2.336145\n",
      "0                        wind_speed  1.338095\n",
      "12                  wind_speed_cube  1.336128\n",
      "11                wind_speed_square  0.955509\n",
      "9                            lambda  0.889607\n",
      "7               pitch_moto_tmp_mean  0.768776\n",
      "10  r_wind_speed_to_generator_speed  0.746467\n",
      "2                      yaw_position  0.574611\n",
      "5                   pitch3_moto_tmp  0.534060\n",
      "3                   pitch1_moto_tmp  0.531009\n",
      "4                   pitch2_moto_tmp  0.523448\n",
      "8                          tmp_diff  0.520308\n",
      "6                  pitch_angle_mean  0.476534\n"
     ]
    }
   ],
   "execution_count": 90
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:40:13.949807Z",
     "start_time": "2024-12-26T01:39:53.871266Z"
    }
   },
   "cell_type": "code",
   "source": "mi_features_21 = univariate_feature_selection(df_21)",
   "id": "426f642a7f512f6d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "根据分数阈值选择的特征及其分数（互信息）:\n",
      "                            Feature     Score\n",
      "1                   generator_speed  2.325910\n",
      "19                  wind_speed_cube  1.494142\n",
      "0                        wind_speed  1.491993\n",
      "13              pitch_moto_tmp_mean  1.212409\n",
      "18                wind_speed_square  1.046452\n",
      "7                   pitch2_moto_tmp  0.981533\n",
      "8                   pitch3_moto_tmp  0.975651\n",
      "6                   pitch1_moto_tmp  0.973217\n",
      "2                      yaw_position  0.938253\n",
      "16                           lambda  0.937859\n",
      "17  r_wind_speed_to_generator_speed  0.862949\n",
      "15                         tmp_diff  0.719639\n",
      "11                 pitch_angle_mean  0.694951\n",
      "9                   environment_tmp  0.667808\n",
      "10                          int_tmp  0.663603\n",
      "3                      pitch1_angle  0.595651\n",
      "14                pitch_moto_tmp_sd  0.590231\n",
      "4                      pitch2_angle  0.585926\n",
      "5                      pitch3_angle  0.575994\n",
      "12                   pitch_angle_sd  0.460754\n"
     ]
    }
   ],
   "execution_count": 91
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:40:13.964945Z",
     "start_time": "2024-12-26T01:40:13.950841Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 交集\n",
    "# chi2_features = set(chi2_features_15).intersection(set(chi2_features_21))\n",
    "# print('卡方检验特征交集')\n",
    "# print(len(chi2_features)) \n",
    "# print(chi2_features)\n",
    "\n",
    "mi_features = set(mi_features_15).intersection(set(mi_features_21))\n",
    "print('互信息特征交集')\n",
    "print(len(mi_features))\n",
    "print(mi_features)"
   ],
   "id": "403e8b037cd28831",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "互信息特征交集\n",
      "13\n",
      "{'pitch_moto_tmp_mean', 'pitch1_moto_tmp', 'wind_speed_square', 'generator_speed', 'lambda', 'wind_speed', 'pitch_angle_mean', 'pitch3_moto_tmp', 'wind_speed_cube', 'yaw_position', 'tmp_diff', 'r_wind_speed_to_generator_speed', 'pitch2_moto_tmp'}\n"
     ]
    }
   ],
   "execution_count": 92
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 逻辑回归",
   "id": "881095b40ac1b2fc"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:40:13.980075Z",
     "start_time": "2024-12-26T01:40:13.965976Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.metrics import classification_report"
   ],
   "id": "eb34bd456dd962b3",
   "outputs": [],
   "execution_count": 93
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:40:13.995298Z",
     "start_time": "2024-12-26T01:40:13.982115Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def logistic_regression_analyse(df, threshold=1):\n",
    "\n",
    "    # 提取特征和标签\n",
    "    X = df.drop('power', axis=1)\n",
    "    y = df['power']\n",
    "    \n",
    "    # 划分训练集和测试集\n",
    "    # X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "    \n",
    "    # 特征标准化\n",
    "    scaler = StandardScaler()\n",
    "    X_train_scaled = scaler.fit_transform(X)\n",
    "    # X_test_scaled = scaler.transform(X_test)\n",
    "    \n",
    "    # 训练逻辑回归模型\n",
    "    model = LinearRegression()\n",
    "    model.fit(X_train_scaled, y)\n",
    "    \n",
    "    # 预测测试集\n",
    "    # y_pred = model.predict(X_test_scaled)\n",
    "    \n",
    "    # 打印分类报告\n",
    "    # print(\"分类报告:\")\n",
    "    # print(classification_report(y_test, y_pred))\n",
    "    \n",
    "    # 提取回归系数\n",
    "    coefficients = pd.DataFrame({\n",
    "        \"Feature\": X.columns,\n",
    "        \"Coefficient\": model.coef_,\n",
    "        \"Abs_Coefficient\": np.abs(model.coef_)  # 计算绝对值\n",
    "    }).sort_values(by=\"Abs_Coefficient\", ascending=False)\n",
    "    \n",
    "    # 打印排序后的特征重要性\n",
    "    print(\"=== 回归系数排序 ===\")\n",
    "    print(coefficients)\n",
    "    \n",
    "    # 设置系数阈值\n",
    "    # threshold = 1\n",
    "    filtered_features = coefficients[coefficients[\"Abs_Coefficient\"] > threshold]\n",
    "    \n",
    "    print(f\"\\n=== 筛选系数绝对值大于 {threshold} 的特征 ===\")\n",
    "    print(filtered_features)\n",
    "    \n",
    "    return sorted(filtered_features[\"Feature\"].tolist())"
   ],
   "id": "15611213403d5700",
   "outputs": [],
   "execution_count": 94
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:40:14.255759Z",
     "start_time": "2024-12-26T01:40:13.996327Z"
    }
   },
   "cell_type": "code",
   "source": [
    "lr_features_15 = logistic_regression_analyse(df_15)\n",
    "lr_features_15"
   ],
   "id": "a89d0f60197af83a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== 回归系数排序 ===\n",
      "                            Feature   Coefficient  Abs_Coefficient\n",
      "27                 pitch_speed_mean -2.384917e+11     2.384917e+11\n",
      "10                     pitch2_speed  8.170096e+10     8.170096e+10\n",
      "9                      pitch1_speed  8.143237e+10     8.143237e+10\n",
      "11                     pitch3_speed  7.604453e+10     7.604453e+10\n",
      "29              pitch_moto_tmp_mean -5.833992e+09     5.833992e+09\n",
      "12                  pitch1_moto_tmp  2.018170e+09     2.018170e+09\n",
      "18                          int_tmp  2.005688e+09     2.005688e+09\n",
      "25                 pitch_angle_mean  1.975313e+09     1.975313e+09\n",
      "17                  environment_tmp -1.936759e+09     1.936759e+09\n",
      "13                  pitch2_moto_tmp  1.915058e+09     1.915058e+09\n",
      "14                  pitch3_moto_tmp  1.904038e+09     1.904038e+09\n",
      "6                      pitch1_angle -6.602139e+08     6.602139e+08\n",
      "8                      pitch3_angle -6.586729e+08     6.586729e+08\n",
      "7                      pitch2_angle -6.567402e+08     6.567402e+08\n",
      "31                         tmp_diff -5.720883e+08     5.720883e+08\n",
      "0                        wind_speed  4.746337e-01     4.746337e-01\n",
      "1                   generator_speed  4.297838e-01     4.297838e-01\n",
      "34                wind_speed_square  3.163780e-01     3.163780e-01\n",
      "35                  wind_speed_cube -2.049196e-01     2.049196e-01\n",
      "30                pitch_moto_tmp_sd  3.467284e-02     3.467284e-02\n",
      "28                   pitch_speed_sd  3.381443e-02     3.381443e-02\n",
      "33  r_wind_speed_to_generator_speed  1.907551e-02     1.907551e-02\n",
      "2                    wind_direction -1.240337e-02     1.240337e-02\n",
      "26                   pitch_angle_sd  1.176335e-02     1.176335e-02\n",
      "15                            acc_x  1.135355e-02     1.135355e-02\n",
      "4                      yaw_position -1.105213e-02     1.105213e-02\n",
      "3               wind_direction_mean -1.001846e-02     1.001846e-02\n",
      "20                   pitch2_ng5_tmp  6.998110e-03     6.998110e-03\n",
      "16                            acc_y -3.270491e-03     3.270491e-03\n",
      "19                   pitch1_ng5_tmp  2.501119e-03     2.501119e-03\n",
      "21                   pitch3_ng5_tmp  2.494905e-03     2.494905e-03\n",
      "5                         yaw_speed -1.967359e-03     1.967359e-03\n",
      "24                    pitch3_ng5_DC  7.557546e-04     7.557546e-04\n",
      "32                           lambda -6.814023e-04     6.814023e-04\n",
      "23                    pitch2_ng5_DC -3.510110e-04     3.510110e-04\n",
      "22                    pitch1_ng5_DC  2.330458e-04     2.330458e-04\n",
      "\n",
      "=== 筛选系数绝对值大于 1 的特征 ===\n",
      "                Feature   Coefficient  Abs_Coefficient\n",
      "27     pitch_speed_mean -2.384917e+11     2.384917e+11\n",
      "10         pitch2_speed  8.170096e+10     8.170096e+10\n",
      "9          pitch1_speed  8.143237e+10     8.143237e+10\n",
      "11         pitch3_speed  7.604453e+10     7.604453e+10\n",
      "29  pitch_moto_tmp_mean -5.833992e+09     5.833992e+09\n",
      "12      pitch1_moto_tmp  2.018170e+09     2.018170e+09\n",
      "18              int_tmp  2.005688e+09     2.005688e+09\n",
      "25     pitch_angle_mean  1.975313e+09     1.975313e+09\n",
      "17      environment_tmp -1.936759e+09     1.936759e+09\n",
      "13      pitch2_moto_tmp  1.915058e+09     1.915058e+09\n",
      "14      pitch3_moto_tmp  1.904038e+09     1.904038e+09\n",
      "6          pitch1_angle -6.602139e+08     6.602139e+08\n",
      "8          pitch3_angle -6.586729e+08     6.586729e+08\n",
      "7          pitch2_angle -6.567402e+08     6.567402e+08\n",
      "31             tmp_diff -5.720883e+08     5.720883e+08\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['environment_tmp',\n",
       " 'int_tmp',\n",
       " 'pitch1_angle',\n",
       " 'pitch1_moto_tmp',\n",
       " 'pitch1_speed',\n",
       " 'pitch2_angle',\n",
       " 'pitch2_moto_tmp',\n",
       " 'pitch2_speed',\n",
       " 'pitch3_angle',\n",
       " 'pitch3_moto_tmp',\n",
       " 'pitch3_speed',\n",
       " 'pitch_angle_mean',\n",
       " 'pitch_moto_tmp_mean',\n",
       " 'pitch_speed_mean',\n",
       " 'tmp_diff']"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 95
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:40:14.379433Z",
     "start_time": "2024-12-26T01:40:14.256805Z"
    }
   },
   "cell_type": "code",
   "source": [
    "lr_features_21 = logistic_regression_analyse(df_21, threshold=0.05)\n",
    "lr_features_21"
   ],
   "id": "bb4647fe02e81d2b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== 回归系数排序 ===\n",
      "                            Feature  Coefficient  Abs_Coefficient\n",
      "8                      pitch3_angle     2.753106         2.753106\n",
      "6                      pitch1_angle    -1.621563         1.621563\n",
      "7                      pitch2_angle    -1.309667         1.309667\n",
      "34                wind_speed_square     0.745911         0.745911\n",
      "35                  wind_speed_cube    -0.600438         0.600438\n",
      "1                   generator_speed     0.528214         0.528214\n",
      "0                        wind_speed     0.486678         0.486678\n",
      "14                  pitch3_moto_tmp     0.369304         0.369304\n",
      "13                  pitch2_moto_tmp    -0.264775         0.264775\n",
      "33  r_wind_speed_to_generator_speed     0.100414         0.100414\n",
      "12                  pitch1_moto_tmp    -0.084357         0.084357\n",
      "25                 pitch_angle_mean    -0.054258         0.054258\n",
      "30                pitch_moto_tmp_sd     0.041008         0.041008\n",
      "31                         tmp_diff    -0.034147         0.034147\n",
      "28                   pitch_speed_sd     0.032634         0.032634\n",
      "2                    wind_direction    -0.024839         0.024839\n",
      "3               wind_direction_mean    -0.021252         0.021252\n",
      "29              pitch_moto_tmp_mean     0.013161         0.013161\n",
      "20                   pitch2_ng5_tmp     0.012720         0.012720\n",
      "26                   pitch_angle_sd     0.010559         0.010559\n",
      "11                     pitch3_speed    -0.009287         0.009287\n",
      "21                   pitch3_ng5_tmp     0.008841         0.008841\n",
      "15                            acc_x     0.008380         0.008380\n",
      "19                   pitch1_ng5_tmp     0.007203         0.007203\n",
      "24                    pitch3_ng5_DC     0.006181         0.006181\n",
      "17                  environment_tmp     0.006153         0.006153\n",
      "23                    pitch2_ng5_DC    -0.004862         0.004862\n",
      "10                     pitch2_speed     0.004759         0.004759\n",
      "18                          int_tmp    -0.004697         0.004697\n",
      "16                            acc_y     0.003587         0.003587\n",
      "4                      yaw_position     0.002860         0.002860\n",
      "5                         yaw_speed    -0.002278         0.002278\n",
      "22                    pitch1_ng5_DC     0.002154         0.002154\n",
      "9                      pitch1_speed     0.001248         0.001248\n",
      "27                 pitch_speed_mean    -0.001219         0.001219\n",
      "32                           lambda    -0.000046         0.000046\n",
      "\n",
      "=== 筛选系数绝对值大于 0.05 的特征 ===\n",
      "                            Feature  Coefficient  Abs_Coefficient\n",
      "8                      pitch3_angle     2.753106         2.753106\n",
      "6                      pitch1_angle    -1.621563         1.621563\n",
      "7                      pitch2_angle    -1.309667         1.309667\n",
      "34                wind_speed_square     0.745911         0.745911\n",
      "35                  wind_speed_cube    -0.600438         0.600438\n",
      "1                   generator_speed     0.528214         0.528214\n",
      "0                        wind_speed     0.486678         0.486678\n",
      "14                  pitch3_moto_tmp     0.369304         0.369304\n",
      "13                  pitch2_moto_tmp    -0.264775         0.264775\n",
      "33  r_wind_speed_to_generator_speed     0.100414         0.100414\n",
      "12                  pitch1_moto_tmp    -0.084357         0.084357\n",
      "25                 pitch_angle_mean    -0.054258         0.054258\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['generator_speed',\n",
       " 'pitch1_angle',\n",
       " 'pitch1_moto_tmp',\n",
       " 'pitch2_angle',\n",
       " 'pitch2_moto_tmp',\n",
       " 'pitch3_angle',\n",
       " 'pitch3_moto_tmp',\n",
       " 'pitch_angle_mean',\n",
       " 'r_wind_speed_to_generator_speed',\n",
       " 'wind_speed',\n",
       " 'wind_speed_cube',\n",
       " 'wind_speed_square']"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 96
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:40:14.394575Z",
     "start_time": "2024-12-26T01:40:14.380435Z"
    }
   },
   "cell_type": "code",
   "source": [
    "lr_features = set(lr_features_15).intersection(set(lr_features_21))\n",
    "print('逻辑回归特征交集')\n",
    "print(len(lr_features)) \n",
    "print(lr_features)"
   ],
   "id": "3abbd4286b36b36f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "逻辑回归特征交集\n",
      "7\n",
      "{'pitch1_moto_tmp', 'pitch_angle_mean', 'pitch1_angle', 'pitch2_angle', 'pitch3_moto_tmp', 'pitch2_moto_tmp', 'pitch3_angle'}\n"
     ]
    }
   ],
   "execution_count": 97
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 随机森林",
   "id": "4875853ba8c7af27"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:40:14.409720Z",
     "start_time": "2024-12-26T01:40:14.395600Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor\n",
    "from sklearn.metrics import classification_report"
   ],
   "id": "49567441286d1848",
   "outputs": [],
   "execution_count": 98
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:40:14.424933Z",
     "start_time": "2024-12-26T01:40:14.410748Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def random_forest_analyse(df, df_test = None, threshold = 1):\n",
    "\n",
    "    # 提取特征和标签\n",
    "    X = df.drop('power', axis=1)\n",
    "    y = df['power']\n",
    "    \n",
    "    # 划分训练集和测试集\n",
    "    \n",
    "    \n",
    "    # 训练随机森林模型\n",
    "    model = RandomForestRegressor(random_state=42)\n",
    "    model.fit(X, y)\n",
    "    \n",
    "    # 预测测试集\n",
    "    # y_pred = model.predict(X_test)\n",
    "    \n",
    "    # 打印分类报告\n",
    "    # print(\"分类报告:\")\n",
    "    # print(classification_report(y_test, y_pred))\n",
    "    \n",
    "    if df_test is not None:\n",
    "        X_test = df_test.drop('power', axis=1)\n",
    "        y_test = df_test['power']\n",
    "        \n",
    "        y_pred = model.predict(X_test)\n",
    "        print(\"另一个数据集的分类报告:\")\n",
    "        print(classification_report(y_test, y_pred))\n",
    "    \n",
    "    # 提取特征重要性\n",
    "    importances = model.feature_importances_  # 随机森林的特征重要性\n",
    "    feature_names = X.columns  # 特征名称\n",
    "    df = pd.DataFrame({'Feature': feature_names, 'Importance': importances})\n",
    "    df.sort_values(by='Importance', ascending=False, inplace=True)\n",
    "    print(df)\n",
    "    return df\n",
    "    # 设置分数阈值\n",
    "      # 设定的分数阈值\n",
    "    \n",
    "    # 根据阈值选择特征\n",
    "    selected_indices = np.where(importances > threshold)[0]\n",
    "    selected_features = feature_names[selected_indices]\n",
    "    selected_importance = importances[selected_indices]\n",
    "    \n",
    "    # 创建 DataFrame 并排序\n",
    "    importance_df = pd.DataFrame({'Feature': selected_features, 'Importance': selected_importance})\n",
    "    importance_df = importance_df.sort_values(by='Importance', ascending=False)\n",
    "    \n",
    "    # 打印结果\n",
    "    print(\"\\n根据分数阈值选择的特征及其重要性（按分数降序排序）:\")\n",
    "    print(importance_df)\n",
    "    \n",
    "    return selected_features"
   ],
   "id": "21d60f4665237f67",
   "outputs": [],
   "execution_count": 99
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T01:57:18.155662Z",
     "start_time": "2024-12-26T01:40:14.425952Z"
    }
   },
   "cell_type": "code",
   "source": "rf_features_15_df = random_forest_analyse(df_15)",
   "id": "eaff0844dceff8b1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            Feature  Importance\n",
      "1                   generator_speed    0.846720\n",
      "0                        wind_speed    0.048771\n",
      "35                  wind_speed_cube    0.040855\n",
      "34                wind_speed_square    0.038572\n",
      "30                pitch_moto_tmp_sd    0.009210\n",
      "31                         tmp_diff    0.004041\n",
      "17                  environment_tmp    0.001030\n",
      "4                      yaw_position    0.000985\n",
      "18                          int_tmp    0.000847\n",
      "2                    wind_direction    0.000829\n",
      "8                      pitch3_angle    0.000739\n",
      "26                   pitch_angle_sd    0.000493\n",
      "29              pitch_moto_tmp_mean    0.000492\n",
      "19                   pitch1_ng5_tmp    0.000484\n",
      "20                   pitch2_ng5_tmp    0.000484\n",
      "21                   pitch3_ng5_tmp    0.000472\n",
      "12                  pitch1_moto_tmp    0.000470\n",
      "3               wind_direction_mean    0.000460\n",
      "25                 pitch_angle_mean    0.000434\n",
      "13                  pitch2_moto_tmp    0.000403\n",
      "6                      pitch1_angle    0.000385\n",
      "14                  pitch3_moto_tmp    0.000366\n",
      "23                    pitch2_ng5_DC    0.000361\n",
      "22                    pitch1_ng5_DC    0.000351\n",
      "24                    pitch3_ng5_DC    0.000349\n",
      "7                      pitch2_angle    0.000271\n",
      "28                   pitch_speed_sd    0.000233\n",
      "33  r_wind_speed_to_generator_speed    0.000222\n",
      "5                         yaw_speed    0.000215\n",
      "32                           lambda    0.000213\n",
      "15                            acc_x    0.000116\n",
      "16                            acc_y    0.000116\n",
      "27                 pitch_speed_mean    0.000004\n",
      "10                     pitch2_speed    0.000003\n",
      "11                     pitch3_speed    0.000002\n",
      "9                      pitch1_speed    0.000001\n"
     ]
    }
   ],
   "execution_count": 100
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:06:21.287100Z",
     "start_time": "2024-12-26T02:06:21.269975Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "select_15_df = rf_features_15_df[rf_features_15_df['Importance'] > 0.0005]\n",
    "print(select_15_df)\n",
    "rf_features_15 = select_15_df['Feature'].tolist()\n",
    "\n",
    "rf_features_15"
   ],
   "id": "e47f436111e72797",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              Feature  Importance\n",
      "1     generator_speed    0.846720\n",
      "0          wind_speed    0.048771\n",
      "35    wind_speed_cube    0.040855\n",
      "34  wind_speed_square    0.038572\n",
      "30  pitch_moto_tmp_sd    0.009210\n",
      "31           tmp_diff    0.004041\n",
      "17    environment_tmp    0.001030\n",
      "4        yaw_position    0.000985\n",
      "18            int_tmp    0.000847\n",
      "2      wind_direction    0.000829\n",
      "8        pitch3_angle    0.000739\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['generator_speed',\n",
       " 'wind_speed',\n",
       " 'wind_speed_cube',\n",
       " 'wind_speed_square',\n",
       " 'pitch_moto_tmp_sd',\n",
       " 'tmp_diff',\n",
       " 'environment_tmp',\n",
       " 'yaw_position',\n",
       " 'int_tmp',\n",
       " 'wind_direction',\n",
       " 'pitch3_angle']"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 108
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:05:09.996940Z",
     "start_time": "2024-12-26T01:57:18.171814Z"
    }
   },
   "cell_type": "code",
   "source": "rf_features_21_df = random_forest_analyse(df_21)\n",
   "id": "ed7075e30cc23da3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            Feature  Importance\n",
      "1                   generator_speed    0.793334\n",
      "34                wind_speed_square    0.060540\n",
      "35                  wind_speed_cube    0.059304\n",
      "0                        wind_speed    0.057414\n",
      "28                   pitch_speed_sd    0.008450\n",
      "31                         tmp_diff    0.002567\n",
      "2                    wind_direction    0.002042\n",
      "30                pitch_moto_tmp_sd    0.001713\n",
      "18                          int_tmp    0.001344\n",
      "4                      yaw_position    0.001273\n",
      "17                  environment_tmp    0.001140\n",
      "29              pitch_moto_tmp_mean    0.001104\n",
      "6                      pitch1_angle    0.000970\n",
      "26                   pitch_angle_sd    0.000919\n",
      "21                   pitch3_ng5_tmp    0.000749\n",
      "20                   pitch2_ng5_tmp    0.000734\n",
      "19                   pitch1_ng5_tmp    0.000716\n",
      "3               wind_direction_mean    0.000664\n",
      "7                      pitch2_angle    0.000563\n",
      "33  r_wind_speed_to_generator_speed    0.000525\n",
      "14                  pitch3_moto_tmp    0.000484\n",
      "12                  pitch1_moto_tmp    0.000460\n",
      "13                  pitch2_moto_tmp    0.000459\n",
      "8                      pitch3_angle    0.000379\n",
      "23                    pitch2_ng5_DC    0.000367\n",
      "22                    pitch1_ng5_DC    0.000360\n",
      "24                    pitch3_ng5_DC    0.000358\n",
      "25                 pitch_angle_mean    0.000331\n",
      "32                           lambda    0.000233\n",
      "5                         yaw_speed    0.000179\n",
      "15                            acc_x    0.000154\n",
      "16                            acc_y    0.000150\n",
      "27                 pitch_speed_mean    0.000013\n",
      "11                     pitch3_speed    0.000003\n",
      "10                     pitch2_speed    0.000002\n",
      "9                      pitch1_speed    0.000002\n"
     ]
    }
   ],
   "execution_count": 102
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:06:51.467747Z",
     "start_time": "2024-12-26T02:06:51.458681Z"
    }
   },
   "cell_type": "code",
   "source": [
    "select_21_df = rf_features_21_df[rf_features_21_df['Importance'] > 0.001]\n",
    "print(select_21_df)\n",
    "rf_features_21 = select_21_df['Feature'].tolist()\n",
    "rf_features_21"
   ],
   "id": "9e01301c5daf893e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                Feature  Importance\n",
      "1       generator_speed    0.793334\n",
      "34    wind_speed_square    0.060540\n",
      "35      wind_speed_cube    0.059304\n",
      "0            wind_speed    0.057414\n",
      "28       pitch_speed_sd    0.008450\n",
      "31             tmp_diff    0.002567\n",
      "2        wind_direction    0.002042\n",
      "30    pitch_moto_tmp_sd    0.001713\n",
      "18              int_tmp    0.001344\n",
      "4          yaw_position    0.001273\n",
      "17      environment_tmp    0.001140\n",
      "29  pitch_moto_tmp_mean    0.001104\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['generator_speed',\n",
       " 'wind_speed_square',\n",
       " 'wind_speed_cube',\n",
       " 'wind_speed',\n",
       " 'pitch_speed_sd',\n",
       " 'tmp_diff',\n",
       " 'wind_direction',\n",
       " 'pitch_moto_tmp_sd',\n",
       " 'int_tmp',\n",
       " 'yaw_position',\n",
       " 'environment_tmp',\n",
       " 'pitch_moto_tmp_mean']"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 109
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:06:56.369828Z",
     "start_time": "2024-12-26T02:06:56.362791Z"
    }
   },
   "cell_type": "code",
   "source": [
    "random_forest_features = set(rf_features_15).intersection(set(rf_features_21))\n",
    "print('随机森林特征交集')\n",
    "print(len(random_forest_features)) \n",
    "print(random_forest_features)"
   ],
   "id": "c42fb39a367cb298",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机森林特征交集\n",
      "10\n",
      "{'int_tmp', 'wind_direction', 'wind_speed_square', 'generator_speed', 'pitch_moto_tmp_sd', 'wind_speed', 'wind_speed_cube', 'yaw_position', 'tmp_diff', 'environment_tmp'}\n"
     ]
    }
   ],
   "execution_count": 110
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 递归特征消除\n",
   "id": "eb0975aef9503172"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:05:10.042415Z",
     "start_time": "2024-12-26T02:05:10.028295Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.feature_selection import RFE\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn.svm import SVC\n"
   ],
   "id": "1f6be00008ac6031",
   "outputs": [],
   "execution_count": 105
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:09:44.932493Z",
     "start_time": "2024-12-26T02:09:44.914265Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def rfe_analyse(df):\n",
    "    \n",
    "    # 提取特征和标签\n",
    "    X = df.drop('power', axis=1)\n",
    "    y = df['power']\n",
    "    \n",
    "    # 划分训练集和测试集\n",
    "    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "    \n",
    "    # 标准化\n",
    "    scaler = StandardScaler()\n",
    "    X_train = scaler.fit_transform(X_train)\n",
    "    X_test = scaler.transform(X_test)\n",
    "    \n",
    "    # 训练逻辑回归模型\n",
    "    # 3. 基模型\n",
    "    model = LinearRegression()\n",
    "    # model.fit(X_train, y_train)\n",
    "    \n",
    "    # 使用 RFE 进行特征选择\n",
    "    n = 15  # 选择的特征数量\n",
    "    rfe = RFE(model, n_features_to_select=n)\n",
    "    fit = rfe.fit(X_train, y_train)\n",
    "    \n",
    "    # 获取选择的特征\n",
    "    selected_features = X.columns[fit.support_]\n",
    "    \n",
    "    # 打印选择的特征\n",
    "    print(\"\\nrfe 选择的特征:\")\n",
    "    print(selected_features)\n",
    "    \n",
    "    return selected_features"
   ],
   "id": "89f050c49443852d",
   "outputs": [],
   "execution_count": 111
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:09:46.915600Z",
     "start_time": "2024-12-26T02:09:45.044627Z"
    }
   },
   "cell_type": "code",
   "source": [
    "rfe_features_15 = rfe_analyse(df_15)\n",
    "rf_features_15"
   ],
   "id": "a4ac3c654eee0224",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "rfe 选择的特征:\n",
      "Index(['wind_speed', 'generator_speed', 'pitch1_angle', 'pitch2_angle',\n",
      "       'pitch3_angle', 'pitch1_moto_tmp', 'pitch2_moto_tmp', 'pitch3_moto_tmp',\n",
      "       'environment_tmp', 'int_tmp', 'pitch_angle_mean', 'pitch_moto_tmp_mean',\n",
      "       'pitch_moto_tmp_sd', 'wind_speed_square', 'wind_speed_cube'],\n",
      "      dtype='object')\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['generator_speed',\n",
       " 'wind_speed',\n",
       " 'wind_speed_cube',\n",
       " 'wind_speed_square',\n",
       " 'pitch_moto_tmp_sd',\n",
       " 'tmp_diff',\n",
       " 'environment_tmp',\n",
       " 'yaw_position',\n",
       " 'int_tmp',\n",
       " 'wind_direction',\n",
       " 'pitch3_angle']"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 112
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:09:50.138541Z",
     "start_time": "2024-12-26T02:09:49.160205Z"
    }
   },
   "cell_type": "code",
   "source": [
    "rfe_features_21 = rfe_analyse(df_21)\n",
    "rf_features_21"
   ],
   "id": "76a8a8f446438585",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "rfe 选择的特征:\n",
      "Index(['wind_speed', 'generator_speed', 'wind_direction', 'pitch1_angle',\n",
      "       'pitch2_angle', 'pitch3_angle', 'pitch1_moto_tmp', 'pitch2_moto_tmp',\n",
      "       'pitch3_moto_tmp', 'pitch_angle_mean', 'pitch_speed_sd',\n",
      "       'pitch_moto_tmp_sd', 'r_wind_speed_to_generator_speed',\n",
      "       'wind_speed_square', 'wind_speed_cube'],\n",
      "      dtype='object')\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['generator_speed',\n",
       " 'wind_speed_square',\n",
       " 'wind_speed_cube',\n",
       " 'wind_speed',\n",
       " 'pitch_speed_sd',\n",
       " 'tmp_diff',\n",
       " 'wind_direction',\n",
       " 'pitch_moto_tmp_sd',\n",
       " 'int_tmp',\n",
       " 'yaw_position',\n",
       " 'environment_tmp',\n",
       " 'pitch_moto_tmp_mean']"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 113
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:10:36.405125Z",
     "start_time": "2024-12-26T02:10:36.390027Z"
    }
   },
   "cell_type": "code",
   "source": [
    "rfe_features = set(rfe_features_15).intersection(set(rfe_features_21))\n",
    "print('RFE特征交集')\n",
    "print(len(rfe_features)) \n",
    "print(rfe_features)"
   ],
   "id": "8442bc5d0610a0e6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RFE特征交集\n",
      "12\n",
      "{'pitch1_moto_tmp', 'wind_speed_square', 'generator_speed', 'wind_speed', 'pitch1_angle', 'pitch2_angle', 'pitch3_moto_tmp', 'pitch_angle_mean', 'pitch_moto_tmp_sd', 'wind_speed_cube', 'pitch2_moto_tmp', 'pitch3_angle'}\n"
     ]
    }
   ],
   "execution_count": 115
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 总交集",
   "id": "1957f1b2ecca743c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:10:38.845967Z",
     "start_time": "2024-12-26T02:10:38.837522Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from collections import Counter\n",
    "\n",
    "# 所有特征放在一个列表中\n",
    "all_features = [\n",
    "    corr_features, mi_features, lr_features, random_forest_features, rfe_features\n",
    "]\n",
    "\n",
    "# 计算每个特征的出现频率\n",
    "feature_counter = Counter()\n",
    "for feature_set in all_features:\n",
    "    feature_counter.update(feature_set)\n",
    "\n",
    "\n",
    "\n",
    "# 按照频率降序排序\n",
    "sorted_features = sorted(feature_counter.items(), key=lambda x: x[1], reverse=True)\n",
    "\n",
    "# 设置频率阈值（出现次数大于等于 3）\n",
    "threshold = 3\n",
    "selected_features = [feature for feature, count in sorted_features if count >= threshold]\n",
    "\n",
    "print('特征出现频率排序')\n",
    "print(sorted_features)\n",
    "print('')\n",
    "print(\"最终选择的特征:\")\n",
    "print(selected_features)"
   ],
   "id": "18b0d1e54d0005cc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "特征出现频率排序\n",
      "[('pitch1_moto_tmp', 4), ('wind_speed_square', 4), ('generator_speed', 4), ('pitch_angle_mean', 4), ('wind_speed_cube', 4), ('wind_speed', 4), ('pitch3_moto_tmp', 4), ('pitch2_moto_tmp', 4), ('pitch2_angle', 3), ('pitch_moto_tmp_sd', 3), ('pitch1_angle', 3), ('pitch3_angle', 3), ('pitch_moto_tmp_mean', 2), ('int_tmp', 2), ('environment_tmp', 2), ('yaw_position', 2), ('tmp_diff', 2), ('acc_x', 1), ('pitch_speed_sd', 1), ('pitch_angle_sd', 1), ('lambda', 1), ('r_wind_speed_to_generator_speed', 1), ('wind_direction', 1)]\n",
      "\n",
      "最终选择的特征:\n",
      "['pitch1_moto_tmp', 'wind_speed_square', 'generator_speed', 'pitch_angle_mean', 'wind_speed_cube', 'wind_speed', 'pitch3_moto_tmp', 'pitch2_moto_tmp', 'pitch2_angle', 'pitch_moto_tmp_sd', 'pitch1_angle', 'pitch3_angle']\n"
     ]
    }
   ],
   "execution_count": 116
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:11:00.191464Z",
     "start_time": "2024-12-26T02:11:00.176042Z"
    }
   },
   "cell_type": "code",
   "source": "print(len(selected_features))",
   "id": "3e4a630c61c4cac1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n"
     ]
    }
   ],
   "execution_count": 117
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:11:01.181056Z",
     "start_time": "2024-12-26T02:11:01.170997Z"
    }
   },
   "cell_type": "code",
   "source": "print(selected_features.sort())",
   "id": "bd90a2a042b8592b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "execution_count": 118
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:11:02.351470Z",
     "start_time": "2024-12-26T02:11:02.345706Z"
    }
   },
   "cell_type": "code",
   "source": "print(selected_features)",
   "id": "ba60a103fa0695a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['generator_speed', 'pitch1_angle', 'pitch1_moto_tmp', 'pitch2_angle', 'pitch2_moto_tmp', 'pitch3_angle', 'pitch3_moto_tmp', 'pitch_angle_mean', 'pitch_moto_tmp_sd', 'wind_speed', 'wind_speed_cube', 'wind_speed_square']\n"
     ]
    }
   ],
   "execution_count": 119
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:11:39.270280Z",
     "start_time": "2024-12-26T02:11:39.256883Z"
    }
   },
   "cell_type": "code",
   "source": "selected_features",
   "id": "5b4948c934a4b3f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['generator_speed',\n",
       " 'pitch1_angle',\n",
       " 'pitch1_moto_tmp',\n",
       " 'pitch2_angle',\n",
       " 'pitch2_moto_tmp',\n",
       " 'pitch3_angle',\n",
       " 'pitch3_moto_tmp',\n",
       " 'pitch_angle_mean',\n",
       " 'pitch_moto_tmp_sd',\n",
       " 'wind_speed',\n",
       " 'wind_speed_cube',\n",
       " 'wind_speed_square']"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 120
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:11:40.197670Z",
     "start_time": "2024-12-26T02:11:40.184582Z"
    }
   },
   "cell_type": "code",
   "source": [
    "mechanism_select_columns = ['environment_tmp', 'int_tmp', 'wind_speed', 'wind_speed_square', 'wind_speed_cube', 'yaw_speed', 'yaw_position', 'pitch_speed_mean', 'pitch_speed_sd', 'power', 'wind_direction', 'wind_direction_mean','tmp_diff']\n",
    "mechanism_select_columns.sort()\n",
    "print(mechanism_select_columns)\n",
    "mechanism_select_columns"
   ],
   "id": "b79e2b8cd17520a5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['environment_tmp', 'int_tmp', 'pitch_speed_mean', 'pitch_speed_sd', 'power', 'tmp_diff', 'wind_direction', 'wind_direction_mean', 'wind_speed', 'wind_speed_cube', 'wind_speed_square', 'yaw_position', 'yaw_speed']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['environment_tmp',\n",
       " 'int_tmp',\n",
       " 'pitch_speed_mean',\n",
       " 'pitch_speed_sd',\n",
       " 'power',\n",
       " 'tmp_diff',\n",
       " 'wind_direction',\n",
       " 'wind_direction_mean',\n",
       " 'wind_speed',\n",
       " 'wind_speed_cube',\n",
       " 'wind_speed_square',\n",
       " 'yaw_position',\n",
       " 'yaw_speed']"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 121
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:11:40.353197Z",
     "start_time": "2024-12-26T02:11:40.349081Z"
    }
   },
   "cell_type": "code",
   "source": "len(mechanism_select_columns)",
   "id": "4d63fbd8c35b79d9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 122
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 取15号特征",
   "id": "1cd591b7298189d9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:11:41.940772Z",
     "start_time": "2024-12-26T02:11:41.924612Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "# 所有特征放在一个列表中\n",
    "all_features_15 = [\n",
    "    corr_features_15.index, mi_features_15, lr_features_15, rfe_features_15\n",
    "]\n",
    "\n",
    "# 计算每个特征的出现频率\n",
    "feature_counter_15 = Counter()\n",
    "for feature_set in all_features_15:\n",
    "    feature_counter_15.update(feature_set)\n",
    "\n",
    "\n",
    "\n",
    "# 按照频率降序排序\n",
    "sorted_features_15 = sorted(feature_counter_15.items(), key=lambda x: x[1], reverse=True)\n",
    "print(sorted_features_15)\n",
    "# 设置频率阈值（出现次数大于等于 3）\n",
    "threshold = 3\n",
    "selected_features_15 = [feature for feature, count in sorted_features_15 if count >= threshold]\n",
    "\n",
    "print('特征出现频率排序')\n",
    "print(sorted_features_15)\n",
    "print('')\n",
    "print(\"最终选择的特征:\")\n",
    "print(len(selected_features_15))\n",
    "print(selected_features_15)"
   ],
   "id": "82216fe3b7699cba",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('pitch_angle_mean', 4), ('pitch1_moto_tmp', 4), ('pitch_moto_tmp_mean', 4), ('pitch3_moto_tmp', 4), ('pitch2_moto_tmp', 4), ('wind_speed', 3), ('generator_speed', 3), ('wind_speed_cube', 3), ('pitch3_angle', 3), ('pitch1_angle', 3), ('pitch2_angle', 3), ('wind_speed_square', 3), ('tmp_diff', 3), ('int_tmp', 3), ('environment_tmp', 3), ('pitch_moto_tmp_sd', 2), ('pitch_speed_sd', 1), ('acc_x', 1), ('pitch_angle_sd', 1), ('acc_y', 1), ('yaw_position', 1), ('lambda', 1), ('r_wind_speed_to_generator_speed', 1), ('pitch1_speed', 1), ('pitch2_speed', 1), ('pitch3_speed', 1), ('pitch_speed_mean', 1)]\n",
      "特征出现频率排序\n",
      "[('pitch_angle_mean', 4), ('pitch1_moto_tmp', 4), ('pitch_moto_tmp_mean', 4), ('pitch3_moto_tmp', 4), ('pitch2_moto_tmp', 4), ('wind_speed', 3), ('generator_speed', 3), ('wind_speed_cube', 3), ('pitch3_angle', 3), ('pitch1_angle', 3), ('pitch2_angle', 3), ('wind_speed_square', 3), ('tmp_diff', 3), ('int_tmp', 3), ('environment_tmp', 3), ('pitch_moto_tmp_sd', 2), ('pitch_speed_sd', 1), ('acc_x', 1), ('pitch_angle_sd', 1), ('acc_y', 1), ('yaw_position', 1), ('lambda', 1), ('r_wind_speed_to_generator_speed', 1), ('pitch1_speed', 1), ('pitch2_speed', 1), ('pitch3_speed', 1), ('pitch_speed_mean', 1)]\n",
      "\n",
      "最终选择的特征:\n",
      "15\n",
      "['pitch_angle_mean', 'pitch1_moto_tmp', 'pitch_moto_tmp_mean', 'pitch3_moto_tmp', 'pitch2_moto_tmp', 'wind_speed', 'generator_speed', 'wind_speed_cube', 'pitch3_angle', 'pitch1_angle', 'pitch2_angle', 'wind_speed_square', 'tmp_diff', 'int_tmp', 'environment_tmp']\n"
     ]
    }
   ],
   "execution_count": 123
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:11:53.492477Z",
     "start_time": "2024-12-26T02:11:53.484415Z"
    }
   },
   "cell_type": "code",
   "source": "sorted(selected_features_15)",
   "id": "1a835177c3e29ce1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['environment_tmp',\n",
       " 'generator_speed',\n",
       " 'int_tmp',\n",
       " 'pitch1_angle',\n",
       " 'pitch1_moto_tmp',\n",
       " 'pitch2_angle',\n",
       " 'pitch2_moto_tmp',\n",
       " 'pitch3_angle',\n",
       " 'pitch3_moto_tmp',\n",
       " 'pitch_angle_mean',\n",
       " 'pitch_moto_tmp_mean',\n",
       " 'tmp_diff',\n",
       " 'wind_speed',\n",
       " 'wind_speed_cube',\n",
       " 'wind_speed_square']"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 124
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 取21号特征",
   "id": "c9edca754305c55e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:11:54.247603Z",
     "start_time": "2024-12-26T02:11:54.240554Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "# 所有特征放在一个列表中\n",
    "all_features_21 = [\n",
    "    corr_features_21.index, mi_features_21, lr_features_21, rfe_features_21\n",
    "]\n",
    "\n",
    "# 计算每个特征的出现频率\n",
    "feature_counter_21 = Counter()\n",
    "for feature_set in all_features_21:\n",
    "    feature_counter_21.update(feature_set)\n",
    "\n",
    "\n",
    "\n",
    "# 按照频率降序排序\n",
    "sorted_features_21 = sorted(feature_counter_21.items(), key=lambda x: x[1], reverse=True)\n",
    "print(sorted_features_21)\n",
    "# 设置频率阈值（出现次数大于等于 3）\n",
    "threshold = 3\n",
    "selected_features_21 = [feature for feature, count in sorted_features_21 if count >= threshold]\n",
    "\n",
    "\n",
    "\n",
    "print('特征出现频率排序')\n",
    "print(sorted_features_21)\n",
    "print('')\n",
    "print(\"最终选择的特征:\")\n",
    "print(len(selected_features_21))\n",
    "print(selected_features_21)\n",
    "sorted(selected_features_21)"
   ],
   "id": "b47c8be99d294b7f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('wind_speed', 4), ('generator_speed', 4), ('wind_speed_square', 4), ('pitch3_moto_tmp', 4), ('pitch2_moto_tmp', 4), ('wind_speed_cube', 4), ('pitch1_moto_tmp', 4), ('pitch3_angle', 4), ('pitch2_angle', 4), ('pitch_angle_mean', 4), ('pitch1_angle', 4), ('r_wind_speed_to_generator_speed', 4), ('pitch_moto_tmp_sd', 3), ('pitch_speed_sd', 2), ('pitch_moto_tmp_mean', 2), ('int_tmp', 2), ('environment_tmp', 2), ('pitch_angle_sd', 2), ('acc_x', 1), ('pitch2_ng5_tmp', 1), ('pitch3_ng5_DC', 1), ('pitch1_ng5_DC', 1), ('yaw_position', 1), ('tmp_diff', 1), ('lambda', 1), ('wind_direction', 1)]\n",
      "特征出现频率排序\n",
      "[('wind_speed', 4), ('generator_speed', 4), ('wind_speed_square', 4), ('pitch3_moto_tmp', 4), ('pitch2_moto_tmp', 4), ('wind_speed_cube', 4), ('pitch1_moto_tmp', 4), ('pitch3_angle', 4), ('pitch2_angle', 4), ('pitch_angle_mean', 4), ('pitch1_angle', 4), ('r_wind_speed_to_generator_speed', 4), ('pitch_moto_tmp_sd', 3), ('pitch_speed_sd', 2), ('pitch_moto_tmp_mean', 2), ('int_tmp', 2), ('environment_tmp', 2), ('pitch_angle_sd', 2), ('acc_x', 1), ('pitch2_ng5_tmp', 1), ('pitch3_ng5_DC', 1), ('pitch1_ng5_DC', 1), ('yaw_position', 1), ('tmp_diff', 1), ('lambda', 1), ('wind_direction', 1)]\n",
      "\n",
      "最终选择的特征:\n",
      "13\n",
      "['wind_speed', 'generator_speed', 'wind_speed_square', 'pitch3_moto_tmp', 'pitch2_moto_tmp', 'wind_speed_cube', 'pitch1_moto_tmp', 'pitch3_angle', 'pitch2_angle', 'pitch_angle_mean', 'pitch1_angle', 'r_wind_speed_to_generator_speed', 'pitch_moto_tmp_sd']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['generator_speed',\n",
       " 'pitch1_angle',\n",
       " 'pitch1_moto_tmp',\n",
       " 'pitch2_angle',\n",
       " 'pitch2_moto_tmp',\n",
       " 'pitch3_angle',\n",
       " 'pitch3_moto_tmp',\n",
       " 'pitch_angle_mean',\n",
       " 'pitch_moto_tmp_sd',\n",
       " 'r_wind_speed_to_generator_speed',\n",
       " 'wind_speed',\n",
       " 'wind_speed_cube',\n",
       " 'wind_speed_square']"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 125
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 总交集",
   "id": "9d005bbb0f08873c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:11:55.049663Z",
     "start_time": "2024-12-26T02:11:55.044636Z"
    }
   },
   "cell_type": "code",
   "source": [
    "all_features = all_features_15 + all_features_21\n",
    "from collections import Counter\n",
    "\n",
    "\n",
    "# 计算每个特征的出现频率\n",
    "feature_counter = Counter()\n",
    "for feature_set in all_features:\n",
    "    feature_counter.update(feature_set)\n",
    "\n",
    "# 按照频率降序排序\n",
    "sorted_features = sorted(feature_counter.items(), key=lambda x: x[1], reverse=True)\n",
    "print(sorted_features)\n",
    "# 设置频率阈值（出现次数大于等于 3）\n",
    "threshold = 5\n",
    "selected_features = [feature for feature, count in sorted_features if count >= threshold]\n",
    "\n",
    "print('特征出现频率排序')\n",
    "print(sorted_features)\n",
    "print('')\n",
    "print(\"最终选择的特征:\")\n",
    "print(len(selected_features))\n",
    "print(selected_features)"
   ],
   "id": "25897dae426fc2be",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('pitch_angle_mean', 8), ('pitch1_moto_tmp', 8), ('pitch3_moto_tmp', 8), ('pitch2_moto_tmp', 8), ('wind_speed', 7), ('generator_speed', 7), ('wind_speed_cube', 7), ('pitch3_angle', 7), ('pitch1_angle', 7), ('pitch2_angle', 7), ('wind_speed_square', 7), ('pitch_moto_tmp_mean', 6), ('pitch_moto_tmp_sd', 5), ('int_tmp', 5), ('environment_tmp', 5), ('r_wind_speed_to_generator_speed', 5), ('tmp_diff', 4), ('pitch_speed_sd', 3), ('pitch_angle_sd', 3), ('acc_x', 2), ('yaw_position', 2), ('lambda', 2), ('acc_y', 1), ('pitch1_speed', 1), ('pitch2_speed', 1), ('pitch3_speed', 1), ('pitch_speed_mean', 1), ('pitch2_ng5_tmp', 1), ('pitch3_ng5_DC', 1), ('pitch1_ng5_DC', 1), ('wind_direction', 1)]\n",
      "特征出现频率排序\n",
      "[('pitch_angle_mean', 8), ('pitch1_moto_tmp', 8), ('pitch3_moto_tmp', 8), ('pitch2_moto_tmp', 8), ('wind_speed', 7), ('generator_speed', 7), ('wind_speed_cube', 7), ('pitch3_angle', 7), ('pitch1_angle', 7), ('pitch2_angle', 7), ('wind_speed_square', 7), ('pitch_moto_tmp_mean', 6), ('pitch_moto_tmp_sd', 5), ('int_tmp', 5), ('environment_tmp', 5), ('r_wind_speed_to_generator_speed', 5), ('tmp_diff', 4), ('pitch_speed_sd', 3), ('pitch_angle_sd', 3), ('acc_x', 2), ('yaw_position', 2), ('lambda', 2), ('acc_y', 1), ('pitch1_speed', 1), ('pitch2_speed', 1), ('pitch3_speed', 1), ('pitch_speed_mean', 1), ('pitch2_ng5_tmp', 1), ('pitch3_ng5_DC', 1), ('pitch1_ng5_DC', 1), ('wind_direction', 1)]\n",
      "\n",
      "最终选择的特征:\n",
      "16\n",
      "['pitch_angle_mean', 'pitch1_moto_tmp', 'pitch3_moto_tmp', 'pitch2_moto_tmp', 'wind_speed', 'generator_speed', 'wind_speed_cube', 'pitch3_angle', 'pitch1_angle', 'pitch2_angle', 'wind_speed_square', 'pitch_moto_tmp_mean', 'pitch_moto_tmp_sd', 'int_tmp', 'environment_tmp', 'r_wind_speed_to_generator_speed']\n"
     ]
    }
   ],
   "execution_count": 126
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-26T02:11:55.794180Z",
     "start_time": "2024-12-26T02:11:55.782871Z"
    }
   },
   "cell_type": "code",
   "source": "sorted(selected_features)",
   "id": "83ecc667d251464d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['environment_tmp',\n",
       " 'generator_speed',\n",
       " 'int_tmp',\n",
       " 'pitch1_angle',\n",
       " 'pitch1_moto_tmp',\n",
       " 'pitch2_angle',\n",
       " 'pitch2_moto_tmp',\n",
       " 'pitch3_angle',\n",
       " 'pitch3_moto_tmp',\n",
       " 'pitch_angle_mean',\n",
       " 'pitch_moto_tmp_mean',\n",
       " 'pitch_moto_tmp_sd',\n",
       " 'r_wind_speed_to_generator_speed',\n",
       " 'wind_speed',\n",
       " 'wind_speed_cube',\n",
       " 'wind_speed_square']"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 127
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "8a73e7e4027bfc7c",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
